<?php
/**
 * 搜索相关类
 * Create by PhpStorm
 * User: lijia
 * Data: 2020/8/10
 * Time: 14:50
 */

namespace app\api\controller;


use app\api\business\SearchBus;

class Search extends UserCommon
{

    public function __construct(){
        parent::__construct();
    }

    /**
     * 添加搜索话题记录
     * @return \think\response\Json
     */
    public function addSearch()
    {
        $keywords = input('param.keywords','','trim');
        if ($keywords == '') return resJson(0,$msg='缺少搜索关键字');
        (new SearchBus())->addSearch($keywords);
        return resJson(200,'ok');
    }

    /**
     * 获取话题搜索记录
     * @return \think\response\Json
     */
    public function getTopicList()
    {
        $list = (new SearchBus())->getTopicList();
        return resJson(200,'ok',$list);
    }


    /**
     * 商品搜索
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function toSearch(){
        $keywords = input('param.keywords','','trim');
        if ($keywords == '') return resJson(0,$msg='缺少搜索关键字');
        $result = (new SearchBus())->searchList($keywords);
        return resJson(200,'ok',$result);
    }


    /**
     * 搜索商品历史记录
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getHistory()
    {
        $result = (new SearchBus())->getHistory();
        return resJson(200,'ok',$result);
    }


    /**
     * 清空我的搜索历史
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function clear(){
        $type = input('param.type');
        (new SearchBus())->clear($type);
        return resJson(200,'清空成功');
    }


}